<template>
  <div class="login-container">
    <div class="one">
      <input type="text" id="tip"  @keyup.enter="login" v-model="userName" placeholder="">
      <i class="icon"></i>
    </div>
    <div class="two">
      <input id="tip" @keyup.enter="login" placeholder="" v-model="password" type="password">
      <i class="icon"></i>
    </div>
    <div class="btn" @click="login">
      登录
    </div>
  </div>
</template>

<script>
import { login, getUserSid } from '@/api/user'
import { rsaDecrypt } from '@/utils/commonutils3'
// import { setSid, getToken, getSid } from '@/utils/auth'
import { setSid } from '@/utils/auth'
import md5 from 'md5'
export default {
  name: 'Login',
  data() {
    return {
      userName: '',
      password: '',
      sid: '',
      loading: false,
      passwordType: 'password'
    }
  },
  watch: {
  },
  created() {
    // if (getToken() && getSid()) {
    //   this.$router.push('/index')
    // } else {
    //   this.login()
    // }
    // console.log(window.location.href, 'window.location.href')
    // console.log(window.location.search, 'window.location.search')
    // const str = window.location.search
    // // this.userName = str.split('=')[1]?.substring(0, 6)
    // this.userName = str.match(/code=(\S*)\//)[1]
    // // this.userName = this.userName.slice(0, -1)
    // console.log(this.userName, 'this.userName')
    // if (this.userName) {
    //   this.login()
    // }
    // if (getToken() && getSid()) {
    //   this.$router.push('/index')
    // } else {
  },
  methods: {

    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then((res) => {
            console.log(rsaDecrypt(res), 'res')
            return
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async login() {
      if (!this.userName || !this.password) {
        return this.$message.error('账号密码不能为空')
      }
      const res = await login({ userName: this.userName, password: md5(this.password) })
      if (res.code === 200) {
        console.log(rsaDecrypt(res.data.data), 'res')
        const data = rsaDecrypt(res.data.data)
        // return
        this.$store.dispatch('user/login', data.token).then(() => {
          // this.$router.push({ path: this.redirect || '/' })
          // this.loading = false
          // this.getUserId(data.sid)
          this.sid = data.sid
          setSid(this.sid)
          this.$router.push('/index')
        }).catch(() => {
          this.loading = false
        })
        // }
      } else {
        console.log('1111');
        return this.$message.error(res.message)
      }
    },
    async getUserId(sid) {
      const res = await getUserSid()
      if (res.code === 200) {
        console.log(res, 'sid')
        const data = res.data
        console.log(data, '用户数据')
        this.sid = data.data
        setSid(this.sid)
        this.$router.push('/index')
      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  background: url(../../assets/img/companyloginbgc.png) no-repeat center 0px;
  background-size: contain;

  .inputClass {
    margin-top: 1.3542rem;
    margin-left: 3.125rem;
    width: 3.7552rem;
    height: .3333rem;
    border-radius: .1667rem;
    background-color: #001743;
    border: 1px solid #0075FF;

  }

  .inputClass:focus {
    border: 1px solid #0075FF;
  }

  .one {
    color: #434343;
    position: relative;
    width: 3.7552rem;
    height: .3333rem;
    margin-top: 1.25rem;
    margin-left: 3.125rem;
    margin-bottom: .2604rem;
    background-color: #001743;
    border: 1px solid #0075FF;
    border-radius: .1667rem;

    input {
      -webkit-appearance: none;
      -moz-appearance: none;
      appearance: none;
      outline: 0;
      width: 100%;
      height: 100%;
      background-color: #001743;
      border: 0px solid #fff;
      border-radius: .1667rem;
      box-shadow: 0 0 5px 2px rgba(0, 0, 0, 0.1);
      padding: 0 .1563rem 0 .3646rem;
      color: #fff;

    }

    label {
      font-size: .1667rem;
      z-index: 2;
      position: absolute;
      left: .5208rem;
      top: 15%;
      color: #fff;
    }

    .icon {
      display: inline-block;
      height: .1667rem;
      width: .1667rem;
      background-image: url(../../assets/img/zhanghao.svg);
      /*这里放置图标的绝对路径*/
      background-repeat: no-repeat;
      background-size: contain;
      position: absolute;
      top: .0521rem;
      left: .1042rem;
      z-index: 2;
    }
  }

  .two {
    color: #434343;
    position: relative;
    width: 3.7552rem;
    height: .3333rem;
    // margin-top: 1.3542rem;
    margin-bottom: .2604rem;
    margin-left: 3.125rem;
    background-color: #001743;
    border: 1px solid #0075FF;
    border-radius: .1667rem;

    input {
      -webkit-appearance: none;
      -moz-appearance: none;
      appearance: none;
      outline: 0;
      width: 100%;
      height: 100%;
      background-color: #001743;
      border: 0px solid #fff;
      border-radius: .1667rem;
      box-shadow: 0 0 5px 2px rgba(0, 0, 0, 0.1);
      padding: 0 .1563rem 0 .3646rem;
      color: #fff;

    }

    label {
      font-size: .1667rem;
      z-index: 2;
      position: absolute;
      left: .5208rem;
      top: 15%;
      color: #fff;
    }

    .icon {
      display: inline-block;
      height: .1667rem;
      width: .1667rem;
      background-image: url(../../assets/img/password.svg);
      /*这里放置图标的绝对路径*/
      background-repeat: no-repeat;
      background-size: contain;
      position: absolute;
      top: .0521rem;
      left: .1042rem;
      z-index: 2;
    }
  }

  .btn {
    width: .7813rem;
    height: .3333rem;
    line-height: .3333rem;
    margin: 0 auto;
    background-color: #001743;
    border: 1px solid #0075FF;
    border-radius: .1667rem;
    color: #fff;
    font-size: .125rem;
    text-align: center;
    cursor: pointer;
  }

}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
